package cn.sh.library.gmwx.sparql;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import cn.sh.library.gmwx.bean.InstanceSearchBean;
import cn.sh.library.gmwx.bean.OrganizationSearchBean;
import cn.sh.library.gmwx.bean.PersonSearchBean;
import cn.sh.library.gmwx.bean.WorkSearchBean;

public class MergeParts {
	public static String organizationSearch(OrganizationSearchBean search) {
		String clause = "";

		if (StringUtils.isNotBlank(search.getType())) {
			if ("会议".equals(search.getType())) {
				clause += "?uri a bf:Meeting . ";
			} else {
				clause += "?uri a shl:Organization ; shl:organizationType '" + search.getType() + "' . ";
			}
		}

		if (StringUtils.isNotBlank(search.getKeyword())) {
			clause += " ?uri skos:prefLabel|skos:altLabel ?keyword . " + "   filter contains(?keyword, '"
					+ search.getKeyword() + "')";
		}

		return clause;
	}

	public static String instanceYearsSearch(List<Map<String, String>> year_list) {
		String clause = "";
		String years = "";

		if (year_list != null) {
			for (int i = 0; i < year_list.size(); i++) {
				String year = ((Map) year_list.get(i)).get("year").toString();
				years += "'" + year + "' ";
			}

			clause += "VALUES ?year {" + years + "} ?uri bf:provisionActivity/bf:date ?year . ";
		}

		return clause;
	}

	public static String instanceSearch(InstanceSearchBean search) {
		String clause = "";
		
		//personUri
		if (StringUtils.isNotBlank(search.getPersonUri())) {
			clause += " { ?uri bf:contribution/bf:agent <" + search.getPersonUri() + "> . } ";
		}
		
		if (StringUtils.isNotBlank(search.getPerson())) {
			clause += " { ?uri bf:responsibilityStatement ?creator . filter (<bif:contains>(?creator, '\"" + search.getPerson() + "\"')) } ";
		}

		if (StringUtils.isNotBlank(search.getWork())) {
			clause += " { ?uri bf:hasSeries/bf:instanceOf|bf:instanceOf <" + search.getWork() + "> . } ";
		}
		
		// 高级检索中, 已不存在年份查询条件, 修改为"起始年"+"结束年"查询条件	(add by CM 20190227)
		/*if (StringUtils.isNotBlank(search.getYear())) {
		 	Pattern pattern = Pattern.compile("[0-9]*");
				if(pattern.matcher(search.getYear()).matches()) {
					clause += " { ?uri bf:provisionActivity/bf:date ?date . filter (xsd:int(?date) = " + search.getYear() + " } ";
				}
		}*/
		
		if (StringUtils.isNotBlank(search.getStartYear()) || StringUtils.isNotBlank(search.getEndYear())) {
			clause += " { ?uri bf:provisionActivity/bf:date ?year ";
			if (StringUtils.isNotBlank(search.getStartYear())) {
				clause += ".FILTER (xsd:int(?year) >= " + search.getStartYear() + ")  ";
			}
			if (StringUtils.isNotBlank(search.getEndYear())) {
				clause += ".FILTER (xsd:int(?year) <= " + search.getEndYear() + ")  ";
			}
			clause += "}";
		}
		
		if (StringUtils.isNotBlank(search.getYear())) {
			clause += " { ?uri bf:provisionActivity/bf:date '" + search.getYear() + "' . } ";
		}
		
		if (StringUtils.isNotBlank(search.getPlaceUri())) {
			clause += " { ?uri bf:provisionActivity/bf:place <" + search.getPlaceUri() + "> . } ";
		}

		if (StringUtils.isNotBlank(search.getPlace())) {
			clause += " { ?uri bf:provisionActivityStatement ?place . " + "filter (<bif:contains>(?place, '\"" + search.getPlace() + "\"')) } ";
		}

		if (StringUtils.isNotBlank(search.getPublisher())) {
			clause += " { ?uri bf:provisionActivityStatement ?publisher . " + "filter (<bif:contains>(?publisher, '\"" + search.getPublisher() + "\"')) } ";
		}

		if (StringUtils.isNotBlank(search.getPublisherUri())) {
			clause += " { ?uri bf:provisionActivity/bf:agent <" + search.getPublisherUri() + "> . } ";
		}

		if (StringUtils.isNotBlank(search.getTitle())) {
			clause += " { ?uri rdfs:label ?label . " + "filter (<bif:contains>(?label, '\"" + search.getTitle() + "\"')) } ";
		}

		if (StringUtils.isNotBlank(search.getCode())) {
			clause += " { ?uri bf:identifiedBy ?codeUri . " + "?codeUri rdf:value ?code . filter (<bif:contains>(?code, '\"" + search.getCode() + "\"')) } ";
		}

		if (StringUtils.isNotBlank(search.getSeries())) {
			clause += " { ?uri bf:hasSeries ?seriesUri . " + "?seriesUri rdfs:label ?series . filter (<bif:contains>(?series, '\"" + search.getSeries() + "\"')) } ";
		}
		
		//索书号
		if(StringUtils.isNotBlank(search.getShelfMark())) {
			clause += " { ?item bf:itemOf ?uri; bf:shelfMark ?shelfMark. ?shelfMark rdfs:label ?shelfLabel . filter (contains(?shelfLabel, '" + search.getShelfMark() + "')) } ";
		}
		
		// 全文检索条件
		if (StringUtils.isNotBlank(search.getFreetext())) {
			clause += " { ?uri rdfs:label ?label . filter (bif:contains(?label, '\"" + search.getFreetext() + "\"')) } " + // getTitle
					" UNION { ?uri bf:responsibilityStatement ?creator . filter (bif:contains(?creator, '\"" + search.getFreetext() + "\"')) } " + // getPerson
					" UNION { ?uri bf:provisionActivityStatement ?place . filter (bif:contains(?place, '\"" + search.getFreetext() + "\"')) } " + // getPlace
					" UNION { ?uri bf:provisionActivityStatement ?publisher . filter (bif:contains(?publisher, '\"" + search.getFreetext() + "\"')) } " + // getPublisher
					" UNION { ?uri bf:hasSeries ?seriesUri . ?seriesUri rdfs:label ?series . filter (bif:contains(?series, '\"" + search.getFreetext() + "\"')) } " + // getSeries
					" UNION { ?uri bf:identifiedBy ?codeUri . ?codeUri rdf:value ?code . filter (bif:contains(?code, '\"" + search.getFreetext() + "\"')) } "; // getCode
			// getYear
			Pattern pattern = Pattern.compile("[0-9]*");
			if(pattern.matcher(search.getFreetext()).matches()) {
				clause += " UNION { ?uri bf:provisionActivity/bf:date ?date  } . filter (?date = '" + search.getFreetext() + "')";
			}
		}
		
		/*
		// "全文检索"+"二次检索"条件
		if (StringUtils.isNotBlank(search.getFreetext().trim()) || StringUtils.isNotBlank(search.getSubFreetext().trim())) {
			String newText = "";
			if(StringUtils.isNotBlank(search.getFreetext().trim()) && StringUtils.isNotBlank(search.getSubFreetext().trim())) {
				newText = search.getFreetext().trim() + " " + search.getSubFreetext().trim();
			} else if(StringUtils.isNotBlank(search.getFreetext().trim()) && !StringUtils.isNotBlank(search.getSubFreetext().trim())) {
				newText = search.getFreetext().trim();
			} else if(!StringUtils.isNotBlank(search.getFreetext().trim()) && StringUtils.isNotBlank(search.getSubFreetext().trim())) {
				newText = search.getSubFreetext().trim();
			}
			String[] textArray = newText.split(" ");
			for (int i = 0; i < textArray.length; i++) {
				clause += " { select ?uri where { " + 
						" { ?uri rdfs:label ?label" + i + " . filter (<bif:contains>(?label" + i + ", '\"" + textArray[i] + "\"')) } " + // getTitle
						" UNION { ?uri bf:responsibilityStatement ?creator" + i + " . filter (<bif:contains>(?creator" + i + ", '\"" + textArray[i] + "\"')) } " + // getPerson
						" UNION { ?uri bf:provisionActivityStatement ?place" + i + " . filter (<bif:contains>(?place" + i + ", '\"" + textArray[i] + "\"')) } " + // getPlace
						" UNION { ?uri bf:provisionActivityStatement ?publisher" + i + " . filter (<bif:contains>(?publisher" + i + ", '\"" + textArray[i] + "\"')) } " + // getPublisher
						" UNION { ?uri bf:hasSeries ?seriesUri" + i + " . ?seriesUri" + i + " rdfs:label ?series" + i + " . filter (<bif:contains>(?series" + i + ", '\"" + textArray[i] + "\"')) } " + // getSeries
						" UNION { ?uri bf:identifiedBy ?codeUri" + i + " . ?codeUri" + i + " rdf:value ?code" + i + " . filter (<bif:contains>(?code" + i + ", '\"" + textArray[i] + "\"')) } "; // getCode
				// getYear
				Pattern pattern = Pattern.compile("[0-9]*");
				if(pattern.matcher(textArray[i]).matches()) {
					clause += " UNION { ?uri bf:provisionActivity/bf:date ?date" + i + " . filter (xsd:int(?date" + i + ") = " + textArray[i] + " } ";
				}
				clause += " } } ";
			}
		}*/
		
		//是否全文图片标记
		if(StringUtils.isNotBlank(search.getFulltextFlg())) {
			// "0":有全文图片 
			if(search.getFulltextFlg().equals("0")) {
				clause += " filter exists{ ?uri shl:image ?image } ";
			}
			// "1":无全文图片
			if(search.getFulltextFlg().equals("1")) {
				clause += " filter not exists{ ?uri shl:image ?image } ";
			}
		}

		return clause;
	}

	public static String workSearch(WorkSearchBean search) {
		String clause = "";

		if (StringUtils.isNotBlank(search.getUris())) {
			String[] uris = search.getUris().split(",");

			String values = "";
			for (int i = 0; i < uris.length; i++) {
				if (StringUtils.isNotBlank(uris[i])) {
					values += "<" + uris[i] + ">";

					if (i < uris.length - 1) {
						values += " ";
					}
				}
			}

			clause += "VALUES ?agent {" + values + "} " + "?uri bf:contribution/bf:agent ?agent .";
		}

		if (StringUtils.isNotBlank(search.getPerson())) {
			clause += "?uri dc:creator ?creator . " + "filter (<bif:contains>(?creator, '\"" + search.getPerson()
					+ "\"'))";
		}

		if (StringUtils.isNotBlank(search.getHasSeries()) && "on".equals(search.getHasSeries())) {

		} else {
			clause += " FILTER NOT EXISTS{?instance bf:hasSeries/bf:instanceOf ?uri .} ";
		}

		if (StringUtils.isNotBlank(search.getAudience())) {
			clause += "?uri bf:intendedAudience/bf:identifiedBy/rdfs:label '" + search.getAudience() + "'@zh-cn . ";
		}

		if (StringUtils.isNotBlank(search.getLanguage())) {
			clause += "?uri bf:language ?lang . ?lang bf:identifiedBy/rdfs:label '" + search.getLanguage()
					+ "'@zh-cn . ?lang bf:identifiedBy ?vu ; bf:part '正文语种'. ";
		}

		if (StringUtils.isNotBlank(search.getGenre())) {
			clause += "?uri bf:genreForm/bf:identifiedBy/rdfs:label '" + search.getGenre() + "'@zh-cn . ";
		}

		if (StringUtils.isNotBlank(search.getTitle())) {
			clause += "?uri bf:title ?bf_title. ?bf_title rdfs:label ?title . "
					+ "filter (<bif:contains>(?title, '\"" + search.getTitle() + "\"'))";
		}
		
	  /*
	  	if (StringUtils.isNotBlank(search.getPerson())) {
			if ("on".equals(search.getPersonPre())) {
			  	clause += "?uri bf:contribution/bf:agent ?agent . SERVICE SILENT <" + search.getRemote() + "> {?agent foaf:name '" + search.getPerson() + "'@chs . } ";
		  	} else {
			  	clause += "?uri dc:creator ?creator . " + "filter (<bif:contains>(?creator, '\"" + search.getPerson() + "\"'))";
		  	}
	  	}
	  */

		if (StringUtils.isNotBlank(search.getPublisher())) {
			clause += "?s a bf:Instance ; " + "     bf:instanceOf ?uri ; "
					+ "     bf:provisionActivityStatement ?publisher . " + "filter (<bif:contains>(?publisher, '\""
					+ search.getPublisher() + "\"'))";
		}

		// 新增year查询条件 @author CM
		if (StringUtils.isNotBlank(search.getYear())) {
			clause += "?s a bf:Instance ; " + "     bf:instanceOf ?uri ; "
					+ "     bf:provisionActivity/bf:date ?year . " + "filter (xsd:int(?year) = " + search.getYear()
					+ ")";
		}

		if (StringUtils.isNotBlank(search.getStartYear())) {
			clause += "?s a bf:Instance ; " + "     bf:instanceOf ?uri ; "
					+ "     bf:provisionActivity/bf:date ?year . " + "filter (xsd:int(?year) >= "
					+ search.getStartYear() + ")";
		}

		if (StringUtils.isNotBlank(search.getEndYear())) {
			clause += "?s a bf:Instance ; " + "     bf:instanceOf ?uri ; "
					+ "     bf:provisionActivity/bf:date ?year . " + "filter (xsd:int(?year) <= " + search.getEndYear()
					+ ")";
		}

		if (StringUtils.isNotBlank(search.getClassification())) {
			clause += "?uri bf:classification/bf:classificationPortion '" + search.getClassification() + "' . ";
		}

		if (StringUtils.isNotBlank(search.getSubject())) {
			clause += "?uri bf:subject/rdfs:label '" + search.getSubject() + "' . ";
		}

		// 全文检索条件 @author CM
		if (StringUtils.isNotBlank(search.getFreetext())) {

			clause += " { ?uri bf:title ?bf_title . ?bf_title rdfs:label ?title . filter (<bif:contains>(?title, '\""
					+ search.getFreetext() + "\"')) } " + // getTitle

					" UNION  { ?uri dc:creator ?creator . filter (<bif:contains>(?creator, '\"" + search.getFreetext()
					+ "\"')) } " + // getPerson

					" UNION  { ?s a bf:Instance ; bf:instanceOf ?uri ; bf:provisionActivityStatement ?publisher . filter (<bif:contains>(?publisher, '\""
					+ search.getFreetext() + "\"')) } " + // getPublisher

					" UNION	{ ?s a bf:Instance ; bf:instanceOf ?uri ; bf:provisionActivity/bf:date ?year . filter (?year = '"
					+ search.getFreetext() + "') } " + // getYear

					" UNION	{ ?uri bf:intendedAudience/bf:identifiedBy/rdfs:label '" + search.getFreetext()
					+ "'@zh-cn . } " + // getAudience

					" UNION	{ ?uri bf:language ?lang . ?lang bf:identifiedBy/rdfs:label '" + search.getFreetext()
					+ "'@zh-cn . ?lang bf:identifiedBy ?vu ; bf:part '正文语种'. } " + // getLanguage

					" UNION	{ ?uri bf:genreForm/bf:identifiedBy/rdfs:label '" + search.getFreetext() + "'@zh-cn . } " + // getGenre

					" UNION	{ ?uri bf:classification/bf:classificationPortion '" + search.getFreetext() + "' . } " + // getClassification

					" UNION	{ ?uri bf:subject/rdfs:label '" + search.getFreetext() + "' . } "; // getSubject
		}

		return clause;
	}
	
	public static String personSearch(PersonSearchBean search) {
		String clause = "";
		
		if(StringUtils.isNotBlank(search.getFreetext())) {
			clause += " {?uri foaf:name ?foafName . ?foafName bif:contains '\"" + search.getFreetext() + "\"' . }"
					+ " union "
					+ " {?uri shl:officialEvent/shl:officialPosition ?posi. ?posi rdfs:label ?posiLabel. ?posiLabel bif:contains '\"" + search.getFreetext() + "\"' . } "
					+ " union "
					+ " {?uri shl:officialEvent ?eve. ?eve shl:relatedOrganization  ?org. ?org bif:contains '\"" + search.getFreetext() + "\"' . } "
					;
		}
		
		if(StringUtils.isNotBlank(search.getName())) {
			clause += " ?uri foaf:name ?foafName . ?foafName bif:contains '\"" + search.getName() + "\"' . ";
		}
		
		if(StringUtils.isNotBlank(search.getOfficialPosition())) {
			clause += " ?uri shl:officialEvent/shl:officialPosition ?posi. ?posi rdfs:label ?posiLabel. ?posiLabel bif:contains '\"" + search.getOfficialPosition() + "\"' . ";
		}
		
		if(StringUtils.isNotBlank(search.getOfficialOrganization())) {
			clause += " ?uri shl:officialEvent ?eve. ?eve shl:relatedOrganization  ?org. ?org bif:contains '\"" + search.getOfficialOrganization() + "\"' . ";
		}
		
		if(StringUtils.isNotBlank(search.getIdentifier())) {
			clause += "?uri shl:officialEvent/dc:identifier '" + search.getIdentifier() + "'";
		}

		if(StringUtils.isNotBlank(search.getBegin())) {
			clause += "?uri shl:officialEvent/shl:temporal/shl:begin ?begin . filter strStarts(?begin, '" + search.getBegin() + "') .";
		}

		if(StringUtils.isNotBlank(search.getEnd())) {
			clause += "?uri shl:officialEvent/shl:temporal/shl:end ?end . filter strStarts(?end, '" + search.getEnd() + "') .";
		}
		
		return clause;
	}
	
}
